package com.wsc.wisdomchina.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.ModuleTabItemDetailActivity;
import com.wsc.wisdomchina.activitity.fourmodule.ModuleEventDetailActivity;
import com.wsc.wisdomchina.adapter.ModuleEventAdapter;
import com.wsc.wisdomchina.adapter.ModuleTabDataAdapter;
import com.wsc.wisdomchina.application.MyApplication;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.javaBean.ModuleEventListBean;
import com.wsc.wisdomchina.javaBean.ModuleTabListBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.notice.activity.NoticeDetailActivity;
import com.wsc.wisdomchina.notice.adapter.NoticeListAdapter;
import com.wsc.wisdomchina.notice.bean.NoticeListBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

import java.util.List;


public class ModuleEventFragment extends Fragment implements ContributorView {
    private PullToRefreshListView lv_today_information;
    private ListView actualListView;
    private final String ModuleEventFragment = ModuleEventFragment.class.getSimpleName();

    private int type;
    private Context mcontext;
    private int skip = 0;
    private final int limit = 6;
    private ContributorPresenter contributorPresenter;
    private final String HOME_NEWS = ModuleEventFragment;
    private List<ModuleEventListBean.ModuleEvent> floorActivityList;
    private ModuleEventAdapter floorInoAdapter;
    private boolean isfresh = false;
    private ProgressBar pb_todayInfo;
    private MyApplication myApplication;
    private String aCacheFlagKey;
    private String menuId;
    private String cidNumber;
    private RelativeLayout mEmpty;


    public static ModuleEventFragment newInstance(Bundle b) {
        ModuleEventFragment selectedOneFragment = new ModuleEventFragment();
        selectedOneFragment.setArguments(b);
        return selectedOneFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        cidNumber = getArguments().getString("cid");
        type=getArguments().getInt("type");
        menuId = getArguments().getString("menuId");
        aCacheFlagKey=ModuleEventFragment+type;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.hotnewsfragment_layout, container, false);
        mcontext = getActivity();
        myApplication=(MyApplication) getActivity().getApplication();
        contributorPresenter = new ContributorPresenter(this, mcontext);
        initView(view);
        return view;
    }

    /**
     * @param view
     * @Description 初始化View控件
     * @author jaily.zhang
     */
    private void initView(View view) {

        lv_today_information = (PullToRefreshListView) view.findViewById(R.id.lv_today_information);
        lv_today_information.setMode(Mode.BOTH);
        pb_todayInfo = (ProgressBar) view.findViewById(R.id.pb_todayInfo);
        pb_todayInfo.setVisibility(View.GONE);
        actualListView = lv_today_information.getRefreshableView();
        mEmpty = (RelativeLayout) view.findViewById(R.id.layout);
        actualListView.setEmptyView(mEmpty);
        actualListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        actualListView.setDivider(null);
        Tools.showRefreshView(lv_today_information);

        lv_today_information.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                // TODO Auto-generated method stub
                // Update the LastUpdatedLabe
                // 处理下拉刷新的业务
                if (refreshView.getCurrentMode().equals(Mode.PULL_FROM_START)) {
                    isfresh = true;
                    Tools.showRefreshView(lv_today_information);
                    skip = 0;
                    contributorPresenter.getModuleEventList(cidNumber,menuId,skip, limit, HOME_NEWS);
                } else {
                    isfresh = false;
                    Tools.showRefreshView(lv_today_information);
                    skip += limit;
                    contributorPresenter.getModuleEventList(cidNumber,menuId,skip, limit, HOME_NEWS);
                }

            }
        });

        actualListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (floorActivityList != null && floorActivityList.size() > 0) {
                    Intent intent = new Intent(mcontext, ModuleEventDetailActivity.class);
                    intent.putExtra("bid", floorActivityList.get(position-1).getId());
                    intent.putExtra("cid",cidNumber);
                    intent.putExtra("type",type);
                    startActivity(intent);
                }
            }
        });
    }

    /**
     * @Description 服务器获取数据
     * @author jaily.zhang
     */
    private void showData() {
        contributorPresenter.getModuleEventList(cidNumber,menuId,skip, limit, HOME_NEWS);
    }


    @Override
    public void onLoadContributorStart(String flag) {
        pb_todayInfo.setVisibility(View.VISIBLE);
    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        pb_todayInfo.setVisibility(View.GONE);
        try {
            if (flag.equals(HOME_NEWS)) {
                Gson gson = new Gson();
                ModuleEventListBean moduleEventList = gson.fromJson(jsonString,ModuleEventListBean.class);
                if(null == moduleEventList){
                    return;
                }

                if (moduleEventList.getCode() == 200) {
                    List<ModuleEventListBean.ModuleEvent> floorActivityListBeen = moduleEventList.getObj();
                    if (floorActivityListBeen != null && floorActivityListBeen.size() > 0) {
                        if (isfresh) {
                            floorActivityList = floorActivityListBeen;
                            floorInoAdapter = new ModuleEventAdapter(mcontext, floorActivityList);
                            actualListView.setAdapter(floorInoAdapter);
                        } else {
                            floorActivityList.addAll(floorActivityListBeen);
                            floorInoAdapter.notifyDataSetChanged();
                        }

                    }
                }
            }
        }catch (Exception e)
        {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
        lv_today_information.onRefreshComplete();
        pb_todayInfo.setVisibility(View.GONE);
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
        pb_todayInfo.setVisibility(View.GONE);
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }


    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (myApplication.getInformationFragmentSelected() != null) {
            if (myApplication.getInformationFragmentSelected().equals(ModuleEventFragment + type)) {
                isfresh = true;
                skip = 0;
                showData();
            }
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}
